/****************************************************
	文件：EdgeDetection.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/13 13:47:42
	功能：边缘检测
*****************************************************/

using System;
using UnityEngine;

namespace RenderingPractice
{
    public class EdgeDetection : PostEffectsBase
    {
        [Range(0.0f, 1.0f), Tooltip("边缘检测强度")]
        public float EdgePower = 0.0f;
        [Tooltip("边缘颜色")]
        public Color EdgeColor = Color.black;
        [Tooltip("背景颜色")]
        public Color BackgroundColor = Color.white;

        public Shader EdgeDetectionShader;
        private Material mEdgeDetectionMat;

        public Material EdgeDetectionMat
        {
            get
            {
                mEdgeDetectionMat = CheckShaderAndCreateMaterial(EdgeDetectionShader, mEdgeDetectionMat);
                return mEdgeDetectionMat;
            }
        }

        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (EdgeDetectionMat != null)
            {
                EdgeDetectionMat.SetFloat("_EdgePower", EdgePower);
                EdgeDetectionMat.SetColor("_EdgeColor", EdgeColor);
                EdgeDetectionMat.SetColor("_BackgroundColor", BackgroundColor);
                Graphics.Blit(source, destination, EdgeDetectionMat);
            }
            else
            {
                Graphics.Blit(source, EdgeDetectionMat);
            }
        }
    }
}
